package com.fanhao.dllo.soundfm.fragment.Infofragmnet;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.fanhao.dllo.soundfm.R;
import com.fanhao.dllo.soundfm.adapter.RadioListAdapter;
import com.fanhao.dllo.soundfm.entity.RadioListResult;
import com.fanhao.dllo.soundfm.fragment.BaseFragment;
import com.fanhao.dllo.soundfm.volley.VolleySingleton;
import com.google.gson.Gson;

import java.util.List;

/**
 * Created by dllo on 16/1/21.
 */
public class RadioListFragment extends BaseFragment {
    private RecyclerView radioListRv;
    private RadioListAdapter radioAdapter;
    private TextView titleRadioLisTv;
    private int type;

    public RadioListFragment(int type) {
        this.type = type;
    }

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_radio_list;
    }

    @Override
    protected void initView() {
        titleRadioLisTv = findView(R.id.tvTitle_radioList);
        radioListRv = findView(R.id.recyclerView_radioList);
        radioListRv.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        radioAdapter = new RadioListAdapter(getActivity());
        radioListRv.setAdapter(radioAdapter);

    }

    @Override
    protected void initData() {
        String url = null;
        switch (type) {
            case 0:
                url = "http://live.ximalaya.com/live-web/v1/getRadiosListByType?pageNum=1&radioType=2&device=android&provinceCode=210000&pageSize=15";
                titleRadioLisTv.setText("本地电台");
                break;
            case 1:
                url = "http://live.ximalaya.com/live-web/v1/getRadiosListByType?pageNum=1&radioType=1&device=android&pageSize=15";
                titleRadioLisTv.setText("国家电台");
                break;
            case 2:
                url = "http://live.ximalaya.com/live-web/v1/getRadiosListByType?pageNum=1&radioType=3&device=android&pageSize=15";
                titleRadioLisTv.setText("网络电台");
                break;
        }
        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        RadioListResult result = gson.fromJson(response,
                                RadioListResult.class);
                        if (result != null) {
                            List<RadioListResult.ResultEntity> resultList = result.getResult();
                            radioAdapter.addResultData(resultList);
                        }
                    }
                },
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        VolleySingleton.getInstance().addRequest(request);
    }
}
